#include <vector>
#include "all.h"

int count = 0;
libco::Mutex s_mutex;
libco::Spinlock spin_mutex;

// 测试互斥锁性能
void test_mutexlock(int times) {

    for(int i = 0; i < times; i++) {
        libco::Mutex::Lock lock(s_mutex);
        LIBCO_LOG_INFO << "current count: " << count;
        ++count;
    }
}

// 测试自旋锁性能
void test_spinlock(int times) {
    for(int i = 0; i < times; i++) {
        libco::Spinlock::Lock lock(spin_mutex);
        LIBCO_LOG_INFO << "current count: " << count;
        ++count;
    }
}

int main(int argc, char *argv[]) {
    std::vector<libco::Thread::ptr> thrs;
    int arg = 100;
    int th_num = 300;
    for(int i = 0; i < th_num; i++) {
        // 带参数的线程用std::bind进行参数绑定
        libco::Thread::ptr thr(new libco::Thread(std::bind(test_spinlock, arg), "thread_" + std::to_string(i)));
        thrs.push_back(thr);
    }

    for(int i = 0; i < th_num; i++) {
        thrs[i]->join();
    }
    
    LIBCO_LOG_INFO << "final count = " << count;
    return 0;
}

